package com.cskaoyan.service;

import com.cskaoyan.bean.data.TopicListData;
import com.cskaoyan.domain.MarketTopic;
import com.cskaoyan.domain.MarketTopicExample;
import com.cskaoyan.mapper.MarketTopicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Override
    public ArrayList<Object> queryList(Integer skip, Integer limit) {
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        criteria.andIdIsNotNull().andDeletedEqualTo(false);
        marketTopicExample.setOrderByClause("'add_time' desc");
        List<MarketTopic> marketTopicList = marketTopicMapper.selectByExample(marketTopicExample).stream().skip(skip).limit(limit).collect(Collectors.toList());
        ArrayList<Object> topicList = new ArrayList<>();
        for (MarketTopic marketTopic : marketTopicList) {
            TopicListData topicListData = new TopicListData();
            topicListData.setId(marketTopic.getId());
            topicListData.setPicUrl(marketTopic.getPicUrl());
            topicListData.setPrice(marketTopic.getPrice());
            topicListData.setReadCount(marketTopic.getReadCount());
            topicListData.setSubtitle(marketTopic.getSubtitle());
            topicListData.setTitle(marketTopic.getTitle());
            topicList.add(topicListData);
        }
        return topicList;
    }

    @Override
    public List<TopicListData> selectAllTopicList() {
        MarketTopicExample topicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andIdIsNotNull().andDeletedEqualTo(false);
        topicExample.setOrderByClause("'add_time' desc");
        List<MarketTopic> marketTopicList = marketTopicMapper.selectByExample(topicExample);
        List<TopicListData> topicList = new ArrayList<>();
        for (MarketTopic marketTopic : marketTopicList) {
            TopicListData topicListData = new TopicListData();
            topicListData.setId(marketTopic.getId());
            topicListData.setPicUrl(marketTopic.getPicUrl());
            topicListData.setPrice(marketTopic.getPrice());
            topicListData.setReadCount(marketTopic.getReadCount());
            topicListData.setSubtitle(marketTopic.getSubtitle());
            topicListData.setTitle(marketTopic.getTitle());
            topicList.add(topicListData);
        }

        return topicList;
    }

    @Override
    public MarketTopic selectTopicById(Integer id) {
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        return marketTopic;
    }

    @Override
    public List<MarketTopic> queryTopicRelated(Integer id) {
        MarketTopicExample topicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andIdNotEqualTo(id).andDeletedEqualTo(false);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(topicExample);
        return marketTopics;
    }
}
